package com.daoeping.dao;

import com.daoeping.data.Bzn_question;
import com.daoeping.domain.JsonData;
import com.daoeping.domain.Page;
import com.daoeping.domain.TableData;
import com.daoeping.util.ConverntJsonData;
import com.daoeping.util.TimeDiff;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.springframework.transaction.annotation.Transactional;



public class Bzn_questionDAO extends AllDaoSupport {
	public JsonData findAllBzn_questionByPage (Page page, TableData tableData , Bzn_question bzn_question) {
		try {
			StringBuilder sb1 = new StringBuilder();
			StringBuilder sb2 = new StringBuilder();
			//sb1.append("select  bzn.title , bzn.content , bzn.usr_user.username,  bzn.id ,bzn.usr_user.id ,bzn.createDatetime , bzn.updateDatetime  from Bzn_question bzn where bzn.flag = '"+bzn_question.getFlag()+"' and (bzn.title like '%"+bzn_question.getTitle()+"%' or bzn.content like '%"+bzn_question.getTitle()+"%') ");
			//sb2.append("select count(bzn) from Bzn_question bzn  where bzn.flag = '"+bzn_question.getFlag()+"'and (bzn.title like '%"+bzn_question.getTitle()+"%' or bzn.content like '%"+bzn_question.getTitle()+"%')");//
			sb1.append("select ques.title TITLE , ques.content CONTENT , quesuser.username USERNAME, ques.id ID, ques.user_id  USERID, ques.create_datetime CREATEDATETIME, "+
			"ques.update_datetime UPDATEDATETIME, quesfol.id FOLLOWID , quesfol.user_id FOLUSERID , quesfol.username folUSERNAME "+
			"from bzn_question ques  left join (select follow.id  , follow.user_id  , follow.question_id , followuser.username "+
			"from bzn_questionfollow follow left join usr_user followuser on followuser.id = follow.user_id  "+
			"where  follow.user_id = "+bzn_question.getUsr_user().getId()+") quesfol on ques.id = quesfol.question_id left join usr_user quesuser on quesuser.id = ques.user_id "+
			"where ques.flag = '"+bzn_question.getFlag()+"' and (ques.title like '%"+bzn_question.getTitle()+"%' or ques.content like '%"+bzn_question.getTitle()+"%')  ");
			sb2.append("select count(bzn) from Bzn_question bzn  where bzn.flag = '"+bzn_question.getFlag()+"'and (bzn.title like '%"+bzn_question.getTitle()+"%' or bzn.content like '%"+bzn_question.getTitle()+"%')");//
			if(page.getFlag()!=null&&page.getFlag().equals("concern")){
				sb1.append(" and ques.user_id = "+bzn_question.getUsr_user().getId());
				sb2.append(" and bzn.usr_user.id = "+bzn_question.getUsr_user().getId());
			}
			if(bzn_question.getBsc_category()!=null&&!bzn_question.getBsc_category().getId().equals("")){
				sb1.append(" and ques.category_id = "+bzn_question.getBsc_category().getId());
				sb2.append(" and bzn.bsc_category.id = "+bzn_question.getBsc_category().getId());
			}
			
			/*if(page.getFlag()!=null&&page.getFlag().equals("concern")){
				sb1.append(" and bzn.usr_user.id = "+bzn_question.getUsr_user().getId());
				sb2.append(" and bzn.usr_user.id = "+bzn_question.getUsr_user().getId());
			}
			if(bzn_question.getBsc_category()!=null&&!bzn_question.getBsc_category().getId().equals("")){
				sb1.append(" and bzn.bsc_category.id = "+bzn_question.getBsc_category().getId());
				sb2.append(" and bzn.bsc_category.id = "+bzn_question.getBsc_category().getId());
			}*/
			sb1.append(" order by ques.id desc");
			List<Object[]> resultlist = (List<Object[]>) this.getPageList(page, sb1.toString());	
			List<Object[]> newlist = new ArrayList<Object[]>();
			for (Object[] objects : resultlist) {
				Object[] newObjects = new Object[9];
				for (int i = 0; i < newObjects.length-2; i++) {
					newObjects[i] = objects[i];
				}
				//List<Object[]> followList = (List<Object[]> )selectDataByHQL("select follow.id from Bzn_questionfollow follow where follow.usr_user.id = "+bzn_question.getUsr_user().getId()+" and follow.bzn_question.id = "+objects[3]);
				if(bzn_question.getUsr_user().getId()==objects[4]){
					newObjects[8] = "noconcern";
				}else if(objects[8]==null){
					newObjects[8] = "concern";
				}else{
					newObjects[7] =objects[7];
					newObjects[8] = "cancelconcern";
				}
				newObjects[5] = new TimeDiff().getTimeDiff(objects[5]+"", objects[6]+"");
				newlist.add(newObjects);
			}
			JSONArray json = new ConverntJsonData().getJsonData(newlist);
			JSONArray newJson = new JSONArray();
			newJson.put(this.getNum(page, sb2.toString()));
			for (int i = 0; i < json.length(); i++) {
				newJson.put(json.get(i));
			}
			JsonData jsonData = new JsonData();
			jsonData.setJsonData(newJson.toString());
			System.out.println(newJson);
			return jsonData;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}
	public Bzn_question findBzn_questionById(Bzn_question bzn_question){
		List<Bzn_question> bzn_questionList = (List<Bzn_question>) this.selectDataByHQL("from Bzn_question bzn where bzn.id = "+bzn_question.getId());
		if(bzn_questionList.size()==1){
			return bzn_questionList.get(0);
		}else{
			return null;
		}
	
	}
	@Transactional
	public Object getPageList(Page page, String sql) {
		int temp = (page.getPage() - 1) * page.getPageSize();
		return pageBzn_questionBySQL(sql , temp ,page.getPageSize() );
		
	}
	@Transactional
	public Object pageBzn_questionBySQL(String hql, int offSet, int pageSize) {
		System.out.println("operation:select,hql:"+hql);
		log.debug("operation:select,hql:"+hql+",offSet:"+offSet+",pageSize:"+pageSize);
		try {
			return sessionFactory.getCurrentSession().createSQLQuery(hql).addScalar("TITLE").addScalar("CONTENT").addScalar("USERNAME").addScalar("ID").addScalar("USERID").
					addScalar("CREATEDATETIME").addScalar("UPDATEDATETIME").addScalar("FOLLOWID").addScalar("FOLUSERID").addScalar("folUSERNAME").
					setFirstResult(offSet).setMaxResults(pageSize).list();
		} catch (Exception e) {
			return false;
		}
		
	}
}